package ru.paswd.demo.android.view.presenters

import moxy.MvpPresenter
import ru.paswd.demo.android.view.components.HistoryStorage
import ru.paswd.demo.android.view.components.SqEquResolver
import ru.paswd.demo.android.view.models.DecisionItem
import ru.paswd.demo.android.view.ui.views.MainView

class MainPresenter(
    private val resolver: SqEquResolver,
    private val historyStorage: HistoryStorage,
) : MvpPresenter<MainView>() {

    override fun onFirstViewAttach() {
        viewState.reset()
    }

    fun onCalculateClicked(a: Int, b: Int, c: Int) {
        val aDouble = a.toDouble()
        val bDouble = b.toDouble()
        val cDouble = c.toDouble()

        val result = resolver.resolve(aDouble, bDouble, cDouble)
        historyStorage.save(
            DecisionItem(aDouble, bDouble, cDouble, result)
        )
        viewState.displayResults(result)
    }

    fun onHistoryClicked() {
        viewState.displayHistory()
    }
}